<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用户信息</title>
    <link rel="stylesheet" href="../../../../start/layui/css/layui.css" media="all">

</head>
<body>
    <div class="layui-fluid" style="padding: 15px">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">
                        用户数据
                    </div>
                    <div class="layui-card-body">
                        <form class="layui-form" action="#"  lay-filter="example">
                            <div class="layui-form-item">
                                <label class="layui-form-label">用户名：</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="tmsName" name="tmsName" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">账号：</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="account" name="account" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">手机号：</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="phonenumber" name="phonenumber" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">密码框：</label>
                                <div class="layui-input-inline">
                                    <input type="password" id="tmsPassword" name="tmsPassword" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid layui-word-aux">6位以上数字和英文混合</div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">权限赋予：</label>
                                <div class="layui-input-block" id="orleCheckbox">

                                </div>
                            </div>
                            <input type="hidden" id="genre" value="添加">
                            <input type="hidden" name="tmsId" id="tmsId" value="">
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit  lay-filter="userSubmit">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                </div>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script src="../../../../start/layui/layui.js"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var $ = layui.$
            ,form = layui.form;

        form.render();

        //发送 ajax补充角色
        $.ajax({
            type: "POST",
            url:  '../../../../RoleCheckbox',
            dataType: "json",
            success: function(data){
                for (var i=0;i<data.length;i++){
                    //往复选框界面添加值
                    $("#orleCheckbox").append("<input type='checkbox' id='"+data[i].roleId+"' name='role' value='"+data[i].roleId+"' title='"+data[i].rolename+"'>");
                }
                form.render('checkbox');
            }
        });

        setTimeout(function(){//设置定时器
            form.render('checkbox');
        },1000);

        form.on('submit(userSubmit)', function(data){

            //获取checkbox[name='like']的值
            var arr = new Array();
            $("input:checkbox[name='role']:checked").each(function(i){
                arr[i] = $(this).val();
            });
            data.field.like = arr.join(",");//将数组合并成字符串

            /*alert("有用")
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。*/
            if ($("#genre").val()=="修改"){
                $.ajax({
                    type: "POST",
                    url:  '../../../../UpdateTmsUser',
                    dataType: "text",
                    data:data.field,
                    success: function(data){
                    }
                });
            }else {
                $.ajax({
                    type: "POST",
                    url:  '../../../../menuAction/addUser.action',
                    dataType: "text",
                    data:data.field,
                    success: function(data){
                    }
                });
            }


            layer.msg($("#genre").val()+'成功,即将关闭...');
            setTimeout(function(){//设置定时器
                window.parent.location.reload();//刷新父页面
                //获取当前窗口名称
                //当你在iframe页面关闭自身时
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },1000);
            return false;
        });

    });
</script>